package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseCompanyService;
import com.cloudlead.commonData.service.TbBankAccountService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.TbBankAccount;
import com.cloudlead.fleetAction.service.TbBankAccountEditSer;
import com.cloudlead.fleetService.vo.TbBankAccountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Jinnoo on 2015/8/13.
 */
@Service
public class TbBankAccountEditSerImpl implements TbBankAccountEditSer{

    @Autowired
    TbBankAccountService tbBankAccountService;
    @Autowired
    BaseCompanyService  baseCompanyService;
    @Override
    public void createBankAccount(TbBankAccountVo vo) {
        TbBankAccount tba = new TbBankAccount();
        if(vo.getBaseCompany()!=null){
            BaseCompany bc = new BaseCompany();
                bc.setId(vo.getBaseCompany());
            tba.setBaseCompany(bc);
        }
        BeanUtils.copyProperties(vo, tba);
        tba.setFlag("0");
        tbBankAccountService.create(tba);
    }

    @Override
    public void editBankAccount(TbBankAccountVo vo) {
        TbBankAccount tba = tbBankAccountService.view(vo.getId());
        //BeanUtils.copyProperties(vo,tba);
        if(vo.getBaseCompany()!=null){//公司
            BaseCompany bc = new BaseCompany();
            bc.setId(vo.getBaseCompany());
            tba.setBaseCompany(bc);
        }
        if(vo.getMoney()!=null&&(!"".equals(vo.getMoney())))//金额
            tba.setMoney(vo.getMoney());
        if(vo.getCommont()!=null&&(!"".equals(vo.getCommont())))//备注
            tba.setCommont(vo.getCommont());
        tbBankAccountService.update(tba);
    }

    @Override
    public TbBankAccountVo findOneBankAccount(TbBankAccountVo tba) {
        TbBankAccount tb=new TbBankAccount();
        BeanUtils.copyProperties(tba, tb);
        TbBankAccount tb1=tbBankAccountService.findOne(tb);
        TbBankAccountVo tb2=new TbBankAccountVo();
        if(tb1.getBaseCompany()!=null)//查询公司信息
            tb1.setBaseCompany(baseCompanyService.view(tb1.getBaseCompany().getId()));
        BeanUtils.copyProperties(tb1, tb2);
        return tb2;
    }

    @Override
    public Page<TbBankAccountVo> findAllBankAccount(TbBankAccountVo tba, int size, int page) {
        TbBankAccountVo tba1 = new TbBankAccountVo();
        if(tba!=null) tba1=tba;
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        //查询条件
        tr.add(new Term("flag","0"));//未删除
        if(tba!=null) {
            if (tba.getAccountName() != null)
                tr.add(new Term("accountName", Operator.LK, "%" + tba.getAccountName() + "%"));
            if (tba.getBaseCompany() != null)
                tr.add(new Term("baseCompanyId", tba.getBaseCompany()));
            if (tba.getId() != null)
                tr.add(new Term("id", tba.getId()));
        }
        Filter filter=new Filter( new Criteria( tr));
            pr.setSize(size);
            pr.setPage(page);
            pr.setFilter(filter);
        Page<TbBankAccount> pb=tbBankAccountService.findAll(pr);
        List<TbBankAccount> lpb=pb.getContent();
        Page<TbBankAccountVo> pb1=new Page<TbBankAccountVo> ();
        List<TbBankAccount> lpb1=new ArrayList<TbBankAccount>();
        for(Iterator<TbBankAccount> it = lpb.iterator();it.hasNext();) {//遍历
            TbBankAccount ta = it.next();
            if(ta.getBaseCompany()!=null)//查询公司信息
                ta.setBaseCompany(baseCompanyService.view(ta.getBaseCompany().getId()));
            lpb1.add(ta);
        }
        pb.setContent(lpb1);
        BeanUtils.copyProperties(pb,pb1);

        return pb1;
    }

    @Override
    public void delBankAccount(String id) {
        TbBankAccount tba = tbBankAccountService.view(id);
        if(tba!=null) {
            tba.setFlag("1");
            tbBankAccountService.update(tba);
        }

    }
}
